<?php
namespace app\index\controller;

use think\Db;
use think\Controller;

class Classall extends Controller
{
    /**
     * @return string
     */
    public function index(){
        if(isset($_GET['classType'])){
            $map['level_2'] = $_GET['classType'];
        }
        if(isset($_GET['id'])){
            $map['id'] = $_GET['id'];
        }
        if(isset($_GET['is_rem'])){
            $map['is_rem'] = 1;
        }
        $map['status'] = 1;
        $ret = [];
        $pageCurrent = isset($_GET['pageCurrent'])?$_GET['pageCurrent']:1;
        $pageSize = isset( $_GET['pageSize'])?$_GET['pageSize']:10;
        if($pageCurrent == 1){
            $pageOffset = 0;
        }else{
            $pageOffset =($pageCurrent-1)*$pageSize;
        }
        $data = Db::table('classall')->where($map)->order('id','desc')->limit($pageOffset,$pageSize)->select();
        $sql = Db::table('classall')->getLastSql();
//        echo $sql;die;
        $count = Db::table('classall')->where($map)->count();
        if(!empty($data)){
            $ret['list'] = $data;
            $ret['count'] = $count;
        }else{
            $ret['list'] = [];
            $ret['count'] = 0;
        }
        $this->result($ret, $code = 0, $msg = 'success', $type = 'json');
    }
    public function save(){
        $arr['title'] = $_POST['title'];
        $arr['img'] = $_POST['img'];
        $arr['desc'] = $_POST['desc'];
        $arr['price'] = $_POST['price'];
        $arr['price_old'] = $_POST['price_old'];
        $arr['people'] = $_POST['people'];
        $arr['type'] = $_POST['type'];
        $arr['level_1'] = $_POST['level_1'];
        $arr['level_2'] = $_POST['level_2'];
        $arr['is_rem'] = $_POST['is_rem'];
        $type = $_POST['fromType'];
        $upId = $_POST['id'];
        $data = '';
        if($type == 'up'){
            $data = Db::table('classall')->where('id',$upId)->update($arr);
        }elseif($type == 'add'){
            $data = Db::table('classall')->insert($arr);
        }
        if($data){
            $msg = 'success';
        }else{
            $msg = 'error';
        }
        $this->result($data, $code = 0, $msg , $type = 'json');
    }
    public function del(){
        $id = $_POST['id'];
        $arr['status'] = 0;
        $data = Db::table('classall')->where('id','in',explode(',',$id))->update($arr);
        if($data){
            $msg = 'success';
        }else{
            $msg = 'error';
        }
        $this->result($data, $code = 0, $msg , $type = 'json');
    }
}
